package com.lilosoft.business.precity.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lilosoft.business.precity.dao.DownDataErrorMapper;
import com.lilosoft.business.precity.dao.UpProAcceptIndividualMapper;
import com.lilosoft.business.precity.dao.UpProAcceptMapper;
import com.lilosoft.business.precity.dao.UpProAcceptUnitMapper;
import com.lilosoft.business.precity.dao.UpProAttachmentMapper;
import com.lilosoft.business.precity.dao.UpProMaterialMapper;
import com.lilosoft.business.precity.dao.UpProProcessMapper;
import com.lilosoft.business.precity.dao.UpProResultMapper;
import com.lilosoft.business.precity.dao.UpProSpecialprocedureMapper;
import com.lilosoft.business.precity.models.*;
import com.lilosoft.core.utils.commons.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 小峰
 * Date: 2019-12-17
 * Time: 19:18
 */
@Service
public class DownDataErrorService {

    @Value("${program.parameter.pagesize}")
    private Integer pageSize;
    @Autowired
    private DownDataErrorMapper dataErrorDao;

    @Autowired
    private UpProMaterialMapper upProMaterialDao;
    @Autowired
    private UpProAttachmentMapper upProAttachmentDao;
    @Autowired
    private UpProResultMapper upProResultDao;
    @Autowired
    private UpProSpecialprocedureMapper upProSpecialprocedureDao;
    @Autowired
    private UpProProcessMapper upProProcessDao;
    @Autowired
    private UpProAcceptUnitMapper upProAcceptUnitDao;
    @Autowired
    private UpProAcceptIndividualMapper upProAcceptIndividualDao;
    @Autowired
    private UpProAcceptMapper upProAcceptDao;

    /**
     * 分页查询处理
     *
     * @param example
     * @param pageNumber
     * @return
     */
    public PageInfo<DownDataError> selectByExample(DownDataErrorExample example, Integer pageNumber) {
        PageHelper.startPage(pageNumber, pageSize);
        List<DownDataError> downDataErrors = dataErrorDao.selectByExample(example);
        return new PageInfo<>(downDataErrors);
    }


    public String invokeErrorInfo(DownDataError model) {
        String caseId = null;
        if ("UP_PRO_ACCEPT".equals(model.getTablesqlname().trim())) {
            caseId = deleteRowByProjectRowguid(model.getTablerowguid());
        } else if ("UP_PRO_ATTACHMENT".equals(model.getTablesqlname().trim())) {
            UpProAttachment upProAttachment = upProAttachmentDao.selectByPrimaryKey(model.getTablerowguid());
            if (null != upProAttachment) {
                UpProMaterial upProMaterial = upProMaterialDao.selectByPrimaryKey(upProAttachment.getObjId());
                if (null != upProMaterial) {
                    caseId = deleteRowByProjectRowguid(upProMaterial.getProjectRowguid());
                }
            }
        } else {
            HashMap<String, String> param = new HashMap<>();
            param.put("tableName",model.getTablesqlname());
            param.put("rowid",model.getTablerowguid());
            HashMap<String, Object> map = dataErrorDao.selectOneByParameryId(param);
            if (null != map) {

                caseId = deleteRowByProjectRowguid(map.get("PROJECT_ROWGUID").toString());
            }
        }
        if (StringUtils.isNotEmpty(caseId)) {
            // dataErrorDao.deleteByPrimaryKey(model.getRowguid());
            model.setDelState(1L);
            dataErrorDao.updateByPrimaryKeySelective(model);
        }
        return caseId;
    }

    /**
     * 删除错误的信息
     *
     * @param rowguid 办件受理信息表(up_pro_accept)主键ROWGUID
     * @return 返回 CaseId
     */
    private String deleteRowByProjectRowguid(String rowguid) {
        if (StringUtils.isEmpty(rowguid)) {
            return null;
        }
        //材料目录信息
        UpProMaterialExample materialExample = new UpProMaterialExample();
        UpProMaterialExample.Criteria criteria = materialExample.createCriteria();
        criteria.andProjectRowguidEqualTo(rowguid);
        List<UpProMaterial> upProMaterials = upProMaterialDao.selectByExample(materialExample);
        UpProAttachmentExample attachmentExample;
        for (UpProMaterial upProMaterial : upProMaterials) {
            //附件相关信息
            attachmentExample = new UpProAttachmentExample();
            UpProAttachmentExample.Criteria criteria1 = attachmentExample.createCriteria();
            criteria1.andObjIdEqualTo(upProMaterial.getRowguid());
            //upProAttachmentDao.deleteByExample(attachmentExample);
            UpProAttachment attachment = new UpProAttachment();
            attachment.setDelState(1);
            upProAttachmentDao.updateByExampleSelective(attachment, attachmentExample);

            //删除 材料目录信息
            //upProMaterialDao.deleteByPrimaryKey(upProMaterial.getRowguid());
            upProMaterial.setDelState(1);
            upProMaterialDao.updateByPrimaryKey(upProMaterial);
        }


        //办件结果信息
        UpProResultExample resultExample = new UpProResultExample();
        UpProResultExample.Criteria criteria1 = resultExample.createCriteria();
        criteria1.andProjectRowguidEqualTo(rowguid);
        //upProResultDao.deleteByExample(resultExample);
        UpProResult upProResult = new UpProResult();
        upProResult.setDelState(1);
        upProResultDao.updateByExampleSelective(upProResult, resultExample);


        //特别程序信息
        UpProSpecialprocedureExample specialprocedureExample = new UpProSpecialprocedureExample();
        UpProSpecialprocedureExample.Criteria criteria2 = specialprocedureExample.createCriteria();
        criteria2.andProjectRowguidEqualTo(rowguid);
        //upProSpecialprocedureDao.deleteByExample(specialprocedureExample);
        UpProSpecialprocedure specialprocedure = new UpProSpecialprocedure();
        specialprocedure.setDelState(1);
        upProSpecialprocedureDao.updateByExampleSelective(specialprocedure, specialprocedureExample);


        //办理过程信息
        UpProProcessExample ProcessExample = new UpProProcessExample();
        UpProProcessExample.Criteria criteria3 = ProcessExample.createCriteria();
        criteria3.andProjectRowguidEqualTo(rowguid);
        //upProProcessDao.deleteByExample(ProcessExample);
        UpProProcess upProProcess = new UpProProcess();
        upProProcess.setDelState(1);
        upProProcessDao.updateByExampleSelective(upProProcess, ProcessExample);

        //申请法人信息
        UpProAcceptUnitExample acceptUnitExample = new UpProAcceptUnitExample();
        UpProAcceptUnitExample.Criteria criteria4 = acceptUnitExample.createCriteria();
        criteria4.andProjectRowguidEqualTo(rowguid);
        //upProAcceptUnitDao.deleteByExample(acceptUnitExample);
        UpProAcceptUnit upProAcceptUnit = new UpProAcceptUnit();
        upProAcceptUnit.setDelState(1);
        upProAcceptUnitDao.updateByExampleSelective(upProAcceptUnit, acceptUnitExample);

        //申请自然人信息
        UpProAcceptIndividualExample individualExample = new UpProAcceptIndividualExample();
        UpProAcceptIndividualExample.Criteria criteria5 = individualExample.createCriteria();
        criteria5.andProjectRowguidEqualTo(rowguid);
        //upProAcceptIndividualDao.deleteByExample(individualExample);
        UpProAcceptIndividual individual = new UpProAcceptIndividual();
        individual.setDelState(1);
        upProAcceptIndividualDao.updateByExampleSelective(individual, individualExample);

        //办件受理信息
        UpProAccept upProAccept = upProAcceptDao.selectByPrimaryKey(rowguid);
        if (null==upProAccept) {
            return null;
        }
        String caseId = upProAccept.getProjectno().substring(2);
        // upProAcceptDao.deleteByPrimaryKey(rowguid);
        upProAccept.setDelState(1);
        upProAcceptDao.updateByPrimaryKey(upProAccept);

        return caseId;
    }
}



